package com.fingard.dsp.bank.directbank.fxb01;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;

import java.util.Date;

public class FXB01Balance extends FXB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        int successCount = 0;
        StringBuilder messageStr = new StringBuilder();
        try {
            while (p_req7506.readRecord()) {
                JSONObject request = new JSONObject();
                JSONObject appHeader = new JSONObject();
                JSONObject body = new JSONObject();
                FXB01Header tjcb01Header = new FXB01Header();
                tjcb01Header.transCode = "300130002";
                tjcb01Header.clientTime = Format.DateTime2Format.format(new Date());
                tjcb01Header.batchID = p_req7506.reqHeader.transSeqID;
                tjcb01Header.pageSize = "1";
                tjcb01Header.branchId = bankFront.org;
                //发银行报文的XML对象
                writeCommonHeader(request, tjcb01Header);
                request.put("APP_HEAD",appHeader);
                body.put("BASE_ACCT_NO",p_req7506.readedDetail.actNum);
                request.put("BODY",body);

                String[] tmpStrRet = sendToBank(request.toJSONString(), p_req7506.reqHeader.transType, p_req7506.reqHeader.transType);

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    JSONObject reponse = JSONObject.parseObject(tmpStrRet[1]);
                    if (reponse != null) {
                        JSONObject tmpHeader = JSONObject.parseObject(reponse.getString("SYS_HEAD"));
                        JSONArray ret = JSONArray.parseArray(tmpHeader.getString("RET"));
                        if(ret!=null && ret.size()>0){
                            JSONObject object = JSONObject.parseObject(ret.get(0).toString());
                            String tmprspmsg = object.getString("RET_MSG");
                            String tmprspcod = object.getString("RET_CODE");
                            if (tmprspcod != null && "000000".equals(tmprspcod)) {
                                JSONObject tmpBody = JSONObject.parseObject(reponse.getString("BODY"));
                                successCount++;
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = tmprspmsg;
                                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                                tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                                tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                                //当前余额
                                String tmpCurrBal = tmpBody.getString("BALANCE");
                                tmpRetDetail.currBal = StringHelper.isNullOrEmpty(tmpCurrBal) ? "0.00" : tmpCurrBal;
                                //可用余额
                                String tmpAvailBal = tmpBody.getString("ACTUAL_BAL");
                                tmpRetDetail.availBal = StringHelper.isNullOrEmpty(tmpAvailBal) ? "0.00" : tmpAvailBal;
                                //冻结余额
                             //   String tmpFrozenBal = null;
                              //  tmpRetDetail.frozenBal = StringHelper.isNullOrEmpty(tmpFrozenBal) ? "0.00" : tmpFrozenBal;
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            } else {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = tmprspmsg;
                                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            }
                        } else {
                            messageStr.append("账号:").append(p_req7506.readedDetail.actNum).append(",查询余额失败：").append("未获取到交易状态码").append(";\r\n");
                        }
                    } else {
                        messageStr.append("账号:").append(p_req7506.readedDetail.actNum).append(",查询余额失败：").append(tmpSbMsg).append(";\r\n");
                    }
                } else {
                    messageStr.append("账号:").append(p_req7506.readedDetail.actNum).append(",查询余额失败：").append(tmpStrRet[0]).append(";\r\n");
                }
            }
            if (successCount > 0) {
                retAts.writeRespCode("0");
                if (messageStr.toString().length() > 0) {
                    retAts.writeRespInfo(messageStr.toString());
                } else {
                    retAts.writeRespInfo("查询成功");
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(messageStr.toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.writeRespCode("-1");
            messageStr.append("查询异常：").append(ex.getMessage()).append("\r\n");
            retAts.writeRespInfo(messageStr.toString());
        } finally {
            if (messageStr.length() > 0) {
                WriteBankLogLn(messageStr.toString());
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
